package com.peach.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peach.pojo.Activity;
import com.peach.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    @GetMapping("/selectAllActivities")
    public String selectAllActivities(Model model,
                                      String activityAuthor,
                                      String activityContent,
                                      @RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
                                      @RequestParam(defaultValue="5",value="pageSize")Integer pageSize){

        //为了程序的严谨性，判断非空：
        if(pageNum == null){
            //设置默认当前页
            pageNum = 1;
        }
        if(pageNum <= 0){
            pageNum = 1;
        }
        if(pageSize == null){
            //设置默认每页显示的数据数
            pageSize = 5;
        }

        PageHelper.startPage(pageNum,pageSize);

        try {

            List<Activity> activities = activityService.selectAllActivities(activityAuthor,activityContent);


            PageInfo<Activity> pageInfo = new PageInfo<Activity>(activities,pageSize);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("activityAuthor",activityAuthor);
            model.addAttribute("activityContent",activityContent);


        }finally {
            PageHelper.clearPage(); //清理 ThreadLocal 存储的分页参数,保证线程安全
        }
        return "/activity/activity_list";
    }

    @GetMapping("/insertOneActivity")
    public String insertOneActivity(){
        return "/activity/activity_add";
    }

    @PostMapping("/toAddActivity")
    public String insertOneActivity1(Activity activity){
        Boolean aBoolean = activityService.insertOneActivity(activity);
       // System.out.println(aBoolean);
        return "redirect:/activity/selectAllActivities";
    }

    @GetMapping("/updateActivity")
    public String updateActivity(Integer activityId,Model model){
        Activity activity = activityService.selectActivityById(activityId);
        System.out.println(activity);
        model.addAttribute("activity",activity);
        return "/activity/activity_update";
    }

    @PostMapping("/toUpdateActivity")
    public String toUpdateActivity(Activity activity){
        Boolean aBoolean = activityService.updateOneActivity(activity);
        System.out.println(aBoolean);
        return "redirect:/activity/selectAllActivities";
    }

    @GetMapping("/deleteOneActivity")
    public String deleteOneActivity(Integer activityId){
       activityService.deleteOneActivity(activityId);
       return "redirect:/activity/selectAllActivities";
    }



    @GetMapping("/updateActivityState0_1")
    public String updateActivityState0_1(Integer activityId,Integer activityUserId){
        activityService.updateActivityState0_1(activityId,activityUserId);
        return "redirect:/index/selectAllActivities2";
    }

    @GetMapping("/updateActivityState1_2")
    public String updateActivityState1_2(Integer activityId){
        activityService.updateActivityState1_2(activityId);
        return "redirect:/index/selectAllActivities3";
    }

}
